package com.tz.farmer.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.enums.ErrorCodeEnum;


import com.tz.farmer.service.FarmerRoleInfoService;
import com.tz.farmer.so.FarmerRoleInfoSo;
import com.tz.farmer.so.FarmerUserInfoSo;
import com.tz.farmer.util.POIStyleImpl;
import com.tz.farmer.util.ResultExM;
import com.tz.farmer.vo.FarmerRoleInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SmartAdminRoleInfoControlller
 * @Description TODO
 * @Creator kaikai
 * @Date 2021/5/11 19:05
 */
@Api(tags = "admin角色管理")
@RestController
@RequestMapping("/role")
@Slf4j
public class FarmerRoleInfoController {

    @Resource
    FarmerRoleInfoService farmerRoleInfoService;

    /**
     * 添加查询列表
     * @param so
     * @return
     */
    @GetMapping("/listConditions")
    @ApiOperation("条件查询角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "currentPage", value = "当前页", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "PageSize", value = "每页条数", dataType = "string", defaultValue = "", required = true)
    })
    public ResultEx  getAdminRoleListByConditions(FarmerRoleInfoSo so){
        return farmerRoleInfoService.getAdminRoleListByConditions(so);
    }

    /**
     * 添加用户时查询列表
     * @param
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("添加用户时使用list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "", value = "", dataType = "", defaultValue = "", required = false)
    })
    public ResultEx  getAdminRoleList(){
        return farmerRoleInfoService.getAdminRoleList();
    }


    /**
     * 添加修改软删除
     * @param so
     * @return
     */
    @PostMapping("/editAdd")
    @ApiOperation("添加修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "flag", value = " 增加 flag = 1; 修改 flag = 2", dataType = "int", defaultValue = "", required = true),
            @ApiImplicitParam(name = "id", value = "自增id", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "roleName", value = "角色名", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "roleText", value = "角色说明", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "funId", value = "权限id（多个逗号隔开）", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "isDelete", value = "是否删除 0：否  1：是", dataType = "int", defaultValue = "", required = true),
            @ApiImplicitParam(name = "isStop", value = "是否停用 0：否  1：是", dataType = "int", defaultValue = "", required = true)
    })
    public ResultEx  updateAdminRoleInfo(@RequestBody  FarmerRoleInfoSo so){
        return farmerRoleInfoService.updateAdminRoleInfo(so);
    }

    /**
     * 下载
     * @param so
     * @return
*/
    @GetMapping("/listConditionsDownLoad")
    @ApiOperation("条件查询角色下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名", dataType = "string", defaultValue = "", required = true)
    })
    public ResultEx getCodeByProductId(@ApiIgnore @Validated FarmerRoleInfoSo so, HttpServletResponse resp) {
        try {

            List<FarmerRoleInfoVo> data = farmerRoleInfoService.getRoleDownLoadList(so);
            if (data.size() <= 0) {
                return ResultExM.builder().makeResultNoData();
            }
            ExportParams exportParams = new ExportParams();
            exportParams.setStyle(POIStyleImpl.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, FarmerRoleInfoVo.class, data);
            // 告诉浏览器用什么软件可以打开此文件
            resp.setHeader("content-Type", "application/vnd.ms-excel");
            // 下载文件的默认名称
            resp.setHeader("Content-Disposition", "attachment;filename=" + new String("角色表".getBytes(StandardCharsets.UTF_8), "iso8859-1") + ".xls");
            //编码
            resp.setCharacterEncoding("UTF-8");
            workbook.write(resp.getOutputStream());
            return null;
        } catch (Exception ex) {
            log.error("导出失败：", ex);
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_ERROR_EXPORT);
        }
    }


    /**
     * 添加查询列表
     * @param so
     * @return
     */
    @GetMapping("/delete")
    @ApiOperation("删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", dataType = "int", defaultValue = "", required = false),
    })
    public ResultEx deleteUser(FarmerUserInfoSo so){
        return farmerRoleInfoService.deleteUser(so);

    }



    /**
     * @description: 权限树
     * @name updateUser
     * @author: xhj
     * @date: 2020/7/12 11:19
     * @version V1.0
     * @return: com.nokia.xnzx.msframe.baseobj.result.ResultEx
     */
    @GetMapping("/getTreeData")
    @ApiOperation("权限树接口")
    @ApiImplicitParams({

    }
    )
    public ResultEx getTreeData() {
        return farmerRoleInfoService.getTreeData();
    }



    /**
     * @description: 查询当前角色拥有的权限
     * @name queryFunByRole
     * @author: xhj
     * @date: 2020/7/14 11:54
     * @version V1.0
     * @return: com.nokia.xnzx.msframe.baseobj.result.ResultEx
     */
    @GetMapping("/queryFunByRole")
    @ApiOperation("根据角色查询权限接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色Id", defaultValue = "")
    }
    )
    public ResultEx queryFunByRole(FarmerUserInfoSo so) {
        return farmerRoleInfoService.queryFunByRole(so);

    }



}
